package com.roper.yygh.cmn.controller;

import com.roper.yygh.cmn.service.DictService;
import com.roper.yygh.common.result.Result;
import com.roper.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Description: 数据字典Controller类
 * Date:      2021/6/30 14:18
 * author:    LPH
 * version:   V1.0
 */
@Api(tags = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
@CrossOrigin
public class DictController {

    @Autowired
    private DictService dictService;

    // 根据id查找父节点的所有子节点
    @ApiOperation(value = "根据id查找父节点的所有子节点")
    @GetMapping(value = "/findByParentId/{id}")
    public Result findByParentId(@PathVariable Long id) {
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    // 导出数据字典的接口
    @GetMapping("exportData")
    public void exportList(HttpServletResponse response) {
        dictService.exportDictData(response);
    }

    // 导入数据字典接口
    @PostMapping("importData")
    public Result importDictData(MultipartFile file) {
        dictService.importData(file);
        return Result.ok();
    }

    // 根据dictcode和value查询
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode, @PathVariable String value) {
        return dictService.getName(dictCode, value);
    }

    // 根据value值进行查询
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value) {
        return dictService.getName("", value);
    }

    // 根据dict_code查询下级节点
    @ApiOperation(value = "根据dict_code查询下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }

}
